﻿using System;
using System.Data;
using System.Collections.Generic;
using SERP.DAO;
namespace SERP.Controllers{
public class SalaryFieldsController : SalaryFieldsControllerBase{
	public SalaryFieldsController()
		: base()
	{
	}
    public static SalaryFieldsEntity GetSalaryFieldById(int salaryFieldId)
    {
        return ListSalaryFieldsStatic.Find(delegate(SalaryFieldsEntity o) { return o.SalaryFieldId == salaryFieldId; });
    }
	private DataTable _Table;
	private SalaryFieldsEntity oSalaryFields;	
	public SalaryFieldsEntity SalaryFieldsObject{
		get{
			return oSalaryFields;
		}
		set{
			oSalaryFields=value;
		}
	}
	public DataTable Table{
		get{
			if(_Table==null&&AllowCaching){				
				_Table=Cache.Get<DataTable>(CACHE_KEY_DS);
			}
			return _Table;
		}
		set{
			_Table=value;
		}
	}
	public void LoadAllDSSalaryFields(bool clearCache){
		bool bForce=clearCache;
		if(bForce||_Table==null)
		{
			DataSet ds=Service.SelectAllDS();
			_Table=ds.Tables[0];
			AddIndexToTable(_Table);
			if(AllowCaching)
				AddCache(CACHE_KEY_DS,_Table);			
		}				
	}	
	public void AddIndexToTable(){
		AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		ResetTableIndex(_Table);
	}
	public void LoadObject(Int32 SalaryFieldId){
		SalaryFieldsEntity oLoad=FindSalaryFields(SalaryFieldId);
		if(oLoad==null){
			LoadAllSalaryFields(true);
			oSalaryFields=FindSalaryFields(SalaryFieldId);
		}
		else{
			oSalaryFields=oLoad;
		}
	}
	public bool AddNew(){
		if(oSalaryFields==null)return false;
		bool result = Service.Save(oSalaryFields);
		if(result){
			AddObjectToCache(oSalaryFields);
		}
		return result;
	}
	public bool Update(){
		if(oSalaryFields==null)return false;
		return Service.Update(oSalaryFields);	
	}
	public bool Delete(){
		if(oSalaryFields==null)return false;		
		bool retvalue = Service.Delete(oSalaryFields);
		if(retvalue){			
			RemoveObjectFromCache(oSalaryFields);
		}
		return retvalue;
	}	
	public bool DeleteByID(Int32 SalaryFieldId){
		bool retvalue = Service.DeleteByID(SalaryFieldId);		
		if(retvalue){
			SalaryFieldsEntity oDeleted=FindSalaryFields(SalaryFieldId);
			RemoveObjectFromCache(oDeleted);
		}
		return retvalue;
	}	
	public void ResetObject(){
		this.oSalaryFields = new SalaryFieldsEntity();
	}
	public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.SalaryFieldsObject,row);
    }
    public SalaryFieldsEntity LoadSalaryFields(Int32 SalaryFieldId){			
		SalaryFieldsEntity o=Service.SelectOne(SalaryFieldId);		
		return o;
	}
	#region Cached
    private static List<SalaryFieldsEntity>_ListSalaryFields;
	private static bool _AllowCaching=true;
	public static bool AllowCaching{
		get{return _AllowCaching;}
		set{AllowCaching=value;}
	}	
	public static void ClearCache(){
			if(!_AllowCaching)return;
			Cache.Remove(CACHE_KEY_DS);
			Cache.Remove(CACHE_KEY_LS);
	}
	private const string CACHE_KEY_DS="SalaryFieldsDS";
	private const string CACHE_KEY_LS="SalaryFieldsLS";
	private void AddObjectToCache(SalaryFieldsEntity newObject){
		if(!AllowCaching)return;
		GetCacheList().Add(newObject);
	}
	private void RemoveObjectFromCache(SalaryFieldsEntity oRemove)
	{
		if(!AllowCaching)return;
		if(oRemove!=null)
		GetCacheList().Remove(oRemove);
	}
	private static List<SalaryFieldsEntity>GetCacheList(){
		if(Cache==null)return default(List<SalaryFieldsEntity>);
		List<SalaryFieldsEntity>ls= Cache.Get<List<SalaryFieldsEntity>>(CACHE_KEY_LS);
		if(ls==null){
			ls = new List<SalaryFieldsEntity>();
			AddCache(CACHE_KEY_LS,ls);
		}
		return ls;
	}	
	public static Dictionary<Int32,SalaryFieldsEntity> GetSalaryFieldsDictionary(){
		Dictionary<Int32,SalaryFieldsEntity> dic=new Dictionary<Int32,SalaryFieldsEntity>();
		foreach(SalaryFieldsEntity o in ListSalaryFieldsStatic){
			dic.Add(o.SalaryFieldId,o);
		}
		return dic;
	}
	public List<SalaryFieldsEntity>ListSalaryFields{
		get{
			return LoadAllSalaryFields(false);
		}
	}
	public static List<SalaryFieldsEntity>ListSalaryFieldsStatic{
		get{
			LoadAllSalaryFields(false);
			return _ListSalaryFields;
		}
	}
	public SalaryFieldsEntity FindSalaryFields(Int32 SalaryFieldId){
		if(AllowCaching){
			_ListSalaryFields=GetCacheList();
		}		
		SalaryFieldsEntity oResult=_ListSalaryFields.Find(delegate(SalaryFieldsEntity oFind){
			bool result=true; 
			result&=oFind.SalaryFieldId.Equals(SalaryFieldId);
			return result;
		 });
		return oResult;
	}
	public static List<SalaryFieldsEntity>LoadAllSalaryFields(bool clearCache){
		_ListSalaryFields = new List<SalaryFieldsEntity>();
		if(AllowCaching){
			_ListSalaryFields=GetCacheList();
		}
		if(_ListSalaryFields==null||_ListSalaryFields.Count==0)
			clearCache=true;
		if(!clearCache)return _ListSalaryFields;
		_ListSalaryFields=new List<SalaryFieldsEntity>(Service.SelectAll());		
		if(AllowCaching)
			AddCache(CACHE_KEY_LS,_ListSalaryFields);
		return _ListSalaryFields;
	}
	#endregion
}
}
